﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Input;
using BZ.Logger;
using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.MachineManager;
using NDK.Motion.MachineResources;
using NDK.Motion.Monitor;

namespace NDK.Module.StationLogic
{
    public class Station_量测转盘 : StationLogic
    {
        #region 通用
        public override event PropertyChangedEventHandler PropertyChanged;

        public static Station_量测转盘 Instance = new Station_量测转盘();
        public Stopwatch SunWay09 = new Stopwatch();
        public string StationName = "Station09转盘旋转";

        protected double pos;
        protected Position TomPos;

        //步骤字段、属性
        private StationStep _Step = StationStep.等待所有工站完成;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _Step;
            }
            set
            {
                if (_Step != value)
                {
                    if (!Common.BP_是否屏蔽计时)
                    {
                        WriteStationCTLog("StationCTLog", $"{_Step}用时为：{swStepTime.ElapsedMilliseconds} ms");
                        swStepTime.Restart();
                    }
                    _Step = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        #endregion

        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init()
        {
            base.Init();
            logBaseInfo = "量测转盘流程";
            FlowCT = new Stopwatch();
            Step = StationStep.等待所有工站完成;
            pos = Machine.pos_转盘初始位_转盘旋转.VelocityProfiles[0].Pos;
            Common.AllStationFinish = false;
            Common.AllStationCanWork = true;
            Common.DicHoleCanWork = new Dictionary<int, bool>();
            Common.DicStationWorkStatus = new Dictionary<int, bool>();
            for (int i = 1; i < 9; i++)
            {
                Common.DicHoleCanWork.Add(i, false);
                Common.DicStationWorkStatus.Add(i, false);
            }
            SunWay09.Start();

        }

        public override void ActionProcess()
        {
            try
            {
                switch (Step)
                {
                    case StationStep.等待所有工站完成:
                        if (Common.AllStationFinish)
                        {
                            Common.AllStationCanWork = false;
                            pos += 45;
                            TomPos = Machine.pos_转盘初始位_转盘旋转.Clone();
                            TomPos.VelocityProfiles[0].Pos = pos;
                            Step = StationStep.等等机械手安全信号;
                        }
                        break;

                    case StationStep.等等机械手安全信号:
                        if (Common.PLCAdr.DicInPutValue[PLCInPut.上料安全信号])
                        {
                            Step = StationStep.触发转盘可以旋转;
                            Common.AppendCtLog09.Log(StationName + "," + "等待上料机械手安全信号" + SunWay09.ElapsedMilliseconds + "ms", LogType.Alert);
                        }
                        break;

                    case StationStep.触发转盘可以旋转:
                        if (MotionControl.AxesMove(TomPos))
                        {
                            Step = StationStep.旋转完成重置信号;
                        }
                        break;

                    case StationStep.旋转完成重置信号:
                        for (int i = 1; i < 9; i++)
                        {
                            Common.ChangeTurnTableStatus(i, false);
                        }
                        Common.AllStationCanWork = true;
                        Step = StationStep.等待所有工站完成;
                        break;

                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                UILog_Error($"[{logBaseInfo}] {ex.Message}");
            }
        }

        public override void EmptyActionProcess()
        {
            ActionProcess();
        }

        public override void GRRActionProcess()
        {
            ActionProcess();
        }

        public enum StationStep
        {
            等待所有工站完成,
            等等机械手安全信号,
            触发转盘可以旋转,
            旋转完成重置信号,
        }
    }

}
